import { createWSClient, httpBatchLink, wsLink } from '@trpc/client'
import { createTRPCReact } from '@trpc/react-query'
import superjson from 'superjson'
import type { AppRouter } from '../../../server/src/index'

export const trpc = createTRPCReact<AppRouter>()

// configure TRPCClient to use WebSockets transport
export const client = trpc.createClient({
  links: [
    wsLink<AppRouter>({
      client: createWSClient({
        // @ts-expect-error import meta
        url: `ws://${import.meta.env.V1_HOST}:${import.meta.env.V1_WS_PORT}`,
      }),
    }),
    httpBatchLink<AppRouter>({
      // @ts-expect-error import meta
      url: `http://${import.meta.env.V1_HOST}:${import.meta.env.V1_HTTP_PORT}/trpc`,
    }),
  ],
  transformer: superjson,
})
